//
//  WXVideoHomeViewModel.m
//  NewGuiYang
//
//  Created by 罗江 on 2018/5/28.
//  Copyright © 2018年 luojiang. All rights reserved.
//

#import "WXVideoHomeViewModel.h"
#import "WXVideoAPIManager.h"
#import "WXPlayBackVideoModel.h"

#import <WXCloudModuleCategories/NSArray+Extension.h>
#import <WXCloudModuleCategories/NSArray+Array.h>
#import <WXCloudModuleCategories/NSDictionary+Safe_EXTERN.h>
#import <WXCloudModuleCategories/NSObject+ArrayModel.h>

#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleModelManager.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleChanelModelManager.h>
#import <WXCloudModuleMediator/WXMediator_EXTERN.h>

#import <WXCloudModuleCacheManager/WXCacheManager.h>
#import <WXCloudModuleCacheManager/WXBaseInteractiveModel.h>

#import <WXCloudModuleBaseConfig/WXCloudModuleNotificationKey.h>
#import <WXCloudModuleBaseConfig/WXUMengAnalyticsManager.h>

#import <WXCloudPluginProgressHUD/WXProgressHUD.h>

@interface WXVideoHomeViewModel ()<WXAPIManagerApiCallBackDelegate,WXAPIManagerParamSource>
@property (nonatomic, strong) WXVideoAPIManager *videoAPIManager;
/** 轮播图模型列表 */
@property (nonatomic, copy, readwrite) NSArray *liveVideoHomeModelList;
/** 功能入口模型列表 */
@property (nonatomic, copy, readwrite) NSArray *playBackVideoHomeModelList;
/** 是否加载下一页 */
@property (nonatomic, assign) BOOL isNextLoad;
@property (nonatomic, assign) BOOL isHadLoadAD;
@end;
@implementation WXVideoHomeViewModel
- (instancetype)init
{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}
/** 配置内部参数 */
- (void)initialize
{
    
    self.isHadLoadAD = NO;
}



#pragma mark -自定义代理
#pragma mark -WXAPIManagerParamSource
- (NSDictionary *)paramsForApi:(WXAPIBaseManager *)manager
{
    NSDictionary *params = @{};
    
    if (manager == self.videoAPIManager) {
        params = @{
                   @"action":@"getguiyang"
                   };
    }
    return params;
}
#pragma mark -WXAPIManagerApiCallBackDelegate
/** 成功的回调 */
- (void)managerCallAPIDidSuccess:(WXAPIBaseManager *)manager
{
    NSDictionary *responseData = [manager fetchDataWithReformer:nil];
    /** 轮播图 */
    if (manager == self.videoAPIManager) {
        
        NSArray *tempHomeArray = responseData[@"data"][@"nav"];
        NSArray *tempListArray = responseData[@"data"][@"list"];
        if (tempHomeArray.count<=0) {
            self.liveVideoHomeModelList = nil;
            
        } else {
            NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:10];
            for (NSDictionary *dict in tempHomeArray) {
                NSMutableDictionary * tempDict = [NSMutableDictionary dictionaryWithDictionary:dict];
                [tempDict setValue:dict[@"Title"] forKey:@"title"];
                [tempDict setValue:dict[@"pic"] forKey:@"iconURL"];
                [tempArray addObject:[tempDict copy]];
            }
            tempHomeArray = [tempArray copy];
            tempHomeArray = [tempHomeArray modifyObjectValue:@"7" forKey:kWXMediatorTableModelCustomCellTypeKey];
            self.liveVideoHomeModelList = [[WXMediator sharedInstance] translateServiceDataToAdapterModelList:tempHomeArray];
        }
        if (tempListArray.count<=0) {
            self.playBackVideoHomeModelList = nil;
            
        } else {
            self.playBackVideoHomeModelList = [WXPlayBackVideoModel jsonToModelListWithDataList:responseData[@"data"][@"list"]];
//            NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:10];
//            for (NSDictionary *dict in tempListArray) {
//                NSMutableDictionary * tempDict = [NSMutableDictionary dictionaryWithDictionary:dict];
//                [tempDict setValue:dict[@"ClassCN"] forKey:@"title"];
//                [tempDict setValue:dict[@"PicUrl"] forKey:@"iconURL"];
//                [tempArray addObject:[tempDict copy]];
//            }
//            tempListArray = [tempArray copy];
//            tempListArray  = [tempListArray  modifyObjectValue:@"7" forKey:kWXMediatorTableModelCustomCellTypeKey];
//            self.playBackVideoHomeModelList = [[WXMediator sharedInstance] translateServiceDataToAdapterModelList:tempListArray];
        }
        self.isRequestVideoDataSuccess = YES;
        
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kWXRequestLocalDataCompleteNotification object:nil];
}
/** 失败回调 */
- (void)managerCallAPIDidFailed:(WXAPIBaseManager *)manager
{
    /** 轮播图 */
    //    NSDictionary *responseData = [manager fetchDataWithReformer:nil];
    //    NSString *text = responseData[@"message"];
    //    [WXProgressHUD showText:text];
    if (manager == self.videoAPIManager) {
        self.isRequestVideoDataSuccess = NO;
        
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kWXRequestLocalDataCompleteNotification object:nil];
}
#pragma mark -公开方法

- (void)requestVideoHomeModelList{
   [self.videoAPIManager loadFirstData];
}

#pragma mark -私有方法

#pragma mark -懒加载


- (WXVideoAPIManager *)videoAPIManager
{
    if (_videoAPIManager == nil) {
        _videoAPIManager = [WXVideoAPIManager new];
        _videoAPIManager.paramSource = self;
        _videoAPIManager.delegate = self;
    }
    return _videoAPIManager;
}


@end
